

<!DOCTYPE html>
<html lang="zh-CN" color-mode=light>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>javaIO流 - 唱跳rap</title>
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="google" content="notranslate" />
  
  <meta name="description" content="io流大纲
什么是Java IO流？Java IO流是...">
  <meta name="author" content="东子">
  <link rel="icon" href="/images/icons/icon-29.png" type="image/png" sizes="16x16">
  <link rel="icon" href="/images/icons/icon-29@2x.png" type="image/png" sizes="32x32">
  <link rel="apple-touch-icon" href="/images/icons/icon-1024.png" sizes="180x180">
  <meta rel="mask-icon" href="/images/icons/stun-logo.svg" color="#333333">
  
    <meta rel="msapplication-TileImage" content="/images/icons/icon-1024.png">
    <meta rel="msapplication-TileColor" content="#000000">
  

  
<link rel="stylesheet" href="/css/style.css">


  
    
<link rel="stylesheet" href="https://at.alicdn.com/t/font_1445822_p6ry5n7lrr.css">

  

  
    
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">

  

  
    
      
        
        
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/xcode.min.css" name="highlight-style" mode="light">

      
        
        
<link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/solarized-dark.min.css" name="highlight-style" mode="dark">

      
  

  <script>
    var CONFIG = window.CONFIG || {};
    var ZHAOO = window.ZHAOO || {};
    CONFIG = {
      isHome: false,
      fancybox: true,
      pjax: false,
      loading: {
        gif: '',
        lottie: ''
      },
      lazyload: {
        enable: true,
        only_post: 'false',
        loading: {
          gif: '',
          lottie: ''
        }
      },
      donate: {
        enable: false,
        alipay: 'https://pic.izhaoo.com/alipay.jpg',
        wechat: 'https://pic.izhaoo.com/wechat.jpg'
      },
      galleries: {
        enable: false
      },
      fab: {
        enable: true,
        always_show: false
      },
      carrier: {
        enable: true
      },
      daovoice: {
        enable: false
      },
      preview: {
        background: {
          default: '',
          api: 'https://api.paugram.com/bing/'
        },
        motto: {
          default: '我在开了灯的床头下，想问问自己的心啊。',
          typing: true,
          api: 'https://v2.jinrishici.com/one.json',
          data_contents: '["data","content"]'
        },
      },
      qrcode: {
        enable: true,
        type: 'url',
        image: 'https://pic.izhaoo.com/weapp-code.jpg',
      },
      toc: {
        enable: true
      },
      scrollbar: {
        type: 'default'
      },
      notification: {
        enable: false,
        delay: 4500,
        list: '',
        page_white_list: '',
        page_black_list: ''
      },
      search: {
        enable: false,
        path: ''
      }
    }
  </script>

  

  

<meta name="generator" content="Hexo 6.3.0"></head>

<body class="lock-screen">
  <div class="loading" id="loading"></div>
  
    


  <nav class="navbar">
    <div class="left">
      
        <i class="iconfont iconhome j-navbar-back-home"></i>
      
      
        <i class="iconfont iconqrcode j-navbar-qrcode"></i>
      
      
        <i class="iconfont iconmoono" id="color-toggle" color-toggle="light"></i>
      
      
    </div>
    <div class="center">javaIO流</div>
    <div class="right">
      <i class="iconfont iconmenu j-navbar-menu"></i>
    </div>
    
      <div id="qrcode-navbar"></div>
    
  </nav>

  
  

<nav class="menu">
  <div class="menu-container">
    <div class="menu-close">
      <i class="iconfont iconbaseline-close-px"></i>
    </div>
    <ul class="menu-content"><li class="menu-item">
        <a href="/ " class="underline "> 首页</a>
      </li><li class="menu-item">
        <a href="/archives/ " class="underline "> 归档</a>
      </li><li class="menu-item">
        <a href="/categories/ " class="underline "> 分类</a>
      </li><li class="menu-item">
        <a href="/about/ " class="underline "> 关于</a>
      </li></ul>
    
      <div class="menu-copyright"><p>Powered by <a target="_blank" href="https://gitee.com/comebackearly">东子</a>  | 爷-青结~~</p></div>
    
  </div>
</nav>
  <main id="main">
  <div class="article-wrap">
    <div class="row container">
      <div class="col-xl-3"></div>
      <div class="col-xl-6"><article class="article">
  <div class="wrap">
    <section class="head">
  <img   class="lazyload" data-original="https://tse1-mm.cn.bing.net/th/id/OIP-C.Zhpcm_vE2N2gYIUVgfNJSgHaEq?pid=ImgDet&rs=1" src=""  draggable="false">
  <div class="head-mask">
    <h1 class="head-title">javaIO流</h1>
    <div class="head-info">
      <span class="post-info-item"><i class="iconfont iconcalendar"></i>五月 10, 2023</span>
      
      <span class="post-info-item"><i class="iconfont iconfont-size"></i>1697</span>
    </div>
  </div>
</section>
    <section class="main">
      <section class="content">
        
        <h1 id="io流"><a href="#io流" class="headerlink" title="io流"></a>io流</h1><h2 id="大纲"><a href="#大纲" class="headerlink" title="大纲"></a>大纲</h2><p><img   class="lazyload" data-original="https://tse2-mm.cn.bing.net/th/id/OIP-C.-Qxz3yV01-6CvILJGdZjIQAAAA?pid=ImgDet&rs=1" src=""  alt="IO流概述和分类 – 小猴子monkey1024的Java教程"></p>
<h2 id="什么是Java-IO流？"><a href="#什么是Java-IO流？" class="headerlink" title="什么是Java IO流？"></a>什么是Java IO流？</h2><p>Java IO流是Java中用于读取和写入数据的一组API。它提供了一种与数据流进行交互的标准方式，可以用于从文件、网络、内存等不同来源读取数据，并将数据写入文件、网络、内存等不同目的地。</p>
<h2 id="如何使用Java-NIO读取和写入文件"><a href="#如何使用Java-NIO读取和写入文件" class="headerlink" title="如何使用Java NIO读取和写入文件"></a>如何使用Java NIO读取和写入文件</h2><p>可以使用Java NIO的ByteBuffer和Channel来读取和写入文件。ByteBuffer是一个可读可写的缓冲区，用于存储数据；Channel是一个连接数据源和目标的通道，可以进行读写操作。在读取文件时，首先创建一个ByteBuffer和FileChannel对象，然后使用FileChannel的read()方法读取数据，将读取到的数据写入ByteBuffer中。在写入文件时，需要将数据写入ByteBuffer中，然后使用FileChannel的write()方法将数据写入文件。最后，关闭FileChannel即可完成读取和写入操作。</p>
<h2 id="如何使用Java-IO流实现网络通信？"><a href="#如何使用Java-IO流实现网络通信？" class="headerlink" title="如何使用Java IO流实现网络通信？"></a>如何使用Java IO流实现网络通信？</h2><ol>
<li>可以使用Java的Socket和ServerSocket来实现网络通信。Socket用于创建客户端，ServerSocket用于创建服务器。客户端通过Socket连接到服务器，服务器使用ServerSocket监听连接请求，并接受客户端的连接。连接建立后，客户端和服务器可以通过输入流和输出流进行通信。在实现网络通信时，需要注意处理可能发生的异常，并正确关闭资源。</li>
</ol>
<h2 id="java-IO中的设计模式"><a href="#java-IO中的设计模式" class="headerlink" title="java IO中的设计模式"></a>java IO中的设计模式</h2><blockquote>
<p><strong>适配器（Adapter Pattern）模式</strong> 主要用于接口互不兼容的类的协调工作，你可以将其联想到我们日常经常使用的电源适配器。</p>
</blockquote>
<p><code>InputStreamReader</code> 和 <code>OutputStreamWriter</code> 就是两个适配器(Adapter)， 同时，它们两个也是字节流和字符流之间的桥梁。<code>InputStreamReader</code> 使用 <code>StreamDecoder</code> （流解码器）对字节进行解码，<strong>实现字节流到字符流的转换，</strong><code>OutputStreamWriter</code> 使用<code>StreamEncoder</code>（流编码器）对字符进行编码，实现字符流到字节流的转换。</p>
<blockquote>
<p><strong>装饰器（Decorator）模式</strong> 可以在不改变原有对象的情况下拓展其功能。</p>
</blockquote>
<p>字节缓冲流这里采用了<strong>装饰器模式</strong>来增强 <code>InputStream</code> 和<code>OutputStream</code>子类对象的功能。</p>
<h2 id="InputStreamReader"><a href="#InputStreamReader" class="headerlink" title="InputStreamReader"></a>InputStreamReader</h2><p>InputStreamReader是Java IO包中的一个类，它是InputStream和Reader之间的转换器。InputStreamReader是Reader的子类，它将字节流（InputStream）转换为字符流（Reader）。</p>
<p>eg:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">try &#123;</span><br><span class="line">    FileInputStream fileInputStream = new FileInputStream(&quot;file.txt&quot;);</span><br><span class="line">    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, &quot;UTF-8&quot;);</span><br><span class="line"></span><br><span class="line">    int data = inputStreamReader.read();</span><br><span class="line">    while (data != -1) &#123;</span><br><span class="line">        char ch = (char) data;</span><br><span class="line">        System.out.print(ch);</span><br><span class="line">        data = inputStreamReader.read();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    inputStreamReader.close();</span><br><span class="line">&#125; catch (IOException e) &#123;</span><br><span class="line">    e.printStackTrace();</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Java-IO模型详解，NIO-BIO-AIO的区别"><a href="#Java-IO模型详解，NIO-BIO-AIO的区别" class="headerlink" title="Java IO模型详解，NIO BIO AIO的区别"></a>Java IO模型详解，NIO BIO AIO的区别</h2><p><img   class="lazyload" data-original="https://images.xiaozhuanlan.com/photo/2020/33b193457c928ae02217480f994814b6.png" src=""  alt="img"></p>
<h2 id="简历"><a href="#简历" class="headerlink" title="简历"></a>简历</h2><p>熟悉java io流，使用java io流做过大文件上传，断点续传，并应用于实际项目中的文件下载。</p>

      </section>
      <section class="extra">
        
          <ul class="copyright">
  
    <li><strong>本文作者：</strong>东子</li>
    <li><strong>本文链接：</strong><a href="https://comebackearly.gitee.io/2023/05/10/io%E6%B5%81/index.html" title="https:&#x2F;&#x2F;comebackearly.gitee.io&#x2F;2023&#x2F;05&#x2F;10&#x2F;io%E6%B5%81&#x2F;index.html">https:&#x2F;&#x2F;comebackearly.gitee.io&#x2F;2023&#x2F;05&#x2F;10&#x2F;io%E6%B5%81&#x2F;index.html</a></li>
    <li><strong>版权声明：</strong>本博客所有文章均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" title="BY-NC-SA" target="_blank" rel="noopener">BY-NC-SA</a> 许可协议，转载请注明出处！</li>
  
</ul>
        
        
        
        
  <nav class="nav">
    <a href="/2023/06/23/hello-world/"><i class="iconfont iconleft"></i>Hello World</a>
    <a href="/2023/03/01/Java%E5%B9%B6%E5%8F%91/">java并发<i class="iconfont iconright"></i></a>
  </nav>

      </section>
      
        <section class="comments">
  
    <div class="btn" id="comments-btn">查看评论</div>
  
  
<div id="valine"></div>
<script defer src="//unpkg.com/valine/dist/Valine.min.js"></script>
<script>
  window.onload = function () {
    var loadValine = function () {
      new Valine({
        el: '#valine',
        app_id: "833C0IwLe9o8QE1pN25Mv59F-gzGzoHsz",
        app_key: "YLQZdn7egrZOmJnwr6YFEw0y",
        placeholder: "雁过留痕",
        avatar: "mp",
        pageSize: "10",
        lang: "zh-CN",
      });
    }
    if ( true ) {
      $("#comments-btn").on("click", function () {
        $(this).hide();
        loadValine();
      });
    } else {
      loadValine();
    }
  };
</script>

</section>
      
    </section>
  </div>
</article></div>
      <div class="col-xl-3">
        
          
  <aside class="toc-wrap">
    <h3 class="toc-title">文章目录：</h3>
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#io%E6%B5%81"><span class="toc-text">io流</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%A7%E7%BA%B2"><span class="toc-text">大纲</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFJava-IO%E6%B5%81%EF%BC%9F"><span class="toc-text">什么是Java IO流？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8Java-NIO%E8%AF%BB%E5%8F%96%E5%92%8C%E5%86%99%E5%85%A5%E6%96%87%E4%BB%B6"><span class="toc-text">如何使用Java NIO读取和写入文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8Java-IO%E6%B5%81%E5%AE%9E%E7%8E%B0%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%EF%BC%9F"><span class="toc-text">如何使用Java IO流实现网络通信？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#java-IO%E4%B8%AD%E7%9A%84%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F"><span class="toc-text">java IO中的设计模式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#InputStreamReader"><span class="toc-text">InputStreamReader</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java-IO%E6%A8%A1%E5%9E%8B%E8%AF%A6%E8%A7%A3%EF%BC%8CNIO-BIO-AIO%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">Java IO模型详解，NIO BIO AIO的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%80%E5%8E%86"><span class="toc-text">简历</span></a></li></ol></li></ol>
  </aside>

        
      </div>
    </div>
  </div>
</main>
  

<footer class="footer">
  <div class="footer-social"><a 
        href="tencent://message/?Menu=yes&uin=2911890696 "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#12B7F5'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconQQ "></i>
      </a><a 
        href="javascript:; "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#09BB07'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  iconwechat-fill "></i>
      </a><a 
        href="https://github.com/nuban "
        target="_blank"
        class="footer-social-item"
        onMouseOver="this.style.color= '#9f7be1'" 
        onMouseOut="this.style.color='#33333D'">
          <i class="iconfont  icongithub-fill "></i>
      </a></div>
  
    <div class="footer-copyright"><p>Powered by <a target="_blank" href="https://gitee.com/comebackearly">东子</a>  | 爷-青结~~</p></div>
  
  <link rel="stylesheet" href="/dist/APlayer.min.css">
  <div id="aplayer"></div>
  <script type="text/javascript" src="/dist/APlayer.min.js"></script>
  <script type="text/javascript" src="/dist/music.js"></script>
  
</footer>
  
      <div class="fab fab-plus">
    <i class="iconfont iconplus"></i>
  </div>
  
  
  
  <div class="fab fab-up">
    <i class="iconfont iconcaret-up"></i>
  </div>
  
  
  
    
<script src="/js/color-mode.js"></script>

  
  
</body>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>





  
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script>




  
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>






  
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>




<script src="/js/utils.js"></script>
<script src="/js/script.js"></script>







  <script>
    (function () {
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      } else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>



  <script defer src='https://hm.baidu.com/hm.js?dda12e485d1b1d3113db8147f482111c'></script>











</html>